<template>
  <page-header-wrapper :title="false">
    <a-card title="售后详情">
      <!-- <template #extra> <a-button type="primary" @click="handleEdit({ id: 1 })">编辑账户</a-button></template> -->
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="订单ID" :span="3"> {{ info?.id }} </a-descriptions-item>
        <a-descriptions-item label="售后类型" :span="3"> {{ info?.svBuClsName }} </a-descriptions-item>
        <a-descriptions-item label="顾客姓名" :span="3"> {{ info?.memName }} </a-descriptions-item>
        <a-descriptions-item label="顾客电话" :span="3"> {{ info?.memPhone }} </a-descriptions-item>
        <a-descriptions-item label="创建时间" :span="3"> {{ info?.orderDate }} </a-descriptions-item>
        <a-descriptions-item label="机型" :span="3"> {{ info?.phoneType }} </a-descriptions-item>
        <a-descriptions-item label="串码" :span="3"> {{ info?.phoneCode }} </a-descriptions-item>
        <a-descriptions-item label="金额" :span="3"> {{ info?.amount }} </a-descriptions-item>
        <a-descriptions-item label="售后状态" :span="3">
          <a-tag color="#82af6f" v-if="info?.status == 0">处理中</a-tag>
          <a-tag color="#F89406" v-if="info?.status == 1">待支付</a-tag>
          <a-tag color="#abbac3" v-if="info?.status == 2">已完成</a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="故障描述" :span="3"> {{ info?.errDesc }} </a-descriptions-item>
        <a-descriptions-item label="留机配件" :span="3"> {{ info?.stoAcc }} </a-descriptions-item>
        <a-descriptions-item label="销售负责" :span="3"> {{ info?.empName }} </a-descriptions-item>
        <a-descriptions-item label="归属店" :span="3"> {{ info?.storeName }} </a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex">
            <a-button type="primary" style="margin-right: 5px" v-if="info?.status == 0" @click="handleFinish()">完成</a-button
            >
            <a-button type="primary" style="margin-right: 5px" v-if="info?.status == 1" @click="handlePayment(info.id)">支付</a-button
            >
            <a-button type="danger" style="margin: 0px 5px" v-if="info?.status == 0" @click="handleDelete()">删除</a-button
            >
            <a-button type="danger" style="margin: 0px 5px" v-if="info?.status == 1 || info?.status == 2" @click="handleBack()">撤销</a-button
            >
            <a-button style="margin: 0px 5px" v-print="printOption">打印</a-button>
            <!-- <a-button type="success" style="margin:0px 5px">打印</a-button> -->
          </div>
        </a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card title="售后跟进记录" style="margin-top: 24px">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="8">
              <a-form-item label="日期">
                <a-input v-model="queryParam.keyword" placeholder="请输入"/>
              </a-form-item>
            </a-col> -->
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" @click="handleAddFollow()" v-if="info?.status == 0">新增跟进</a-button>
        <!-- <a-button icon="cloud-upload">导出</a-button> -->
      </div>

      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data-source="loadData"
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="recType" slot-scope="text, record">
          <template>
            <div v-if="record?.recType == 0">文字跟进</div>
            <div v-if="record?.recType == 1">材料跟进</div>
            <div v-if="record?.recType == 2">业务跟进</div>
          </template>
        </span>
        <span slot="status" slot-scope="text, record">
          <template>
            <a-tag color="#82af6f" v-if="record?.status == 1">已审核</a-tag>
            <a-tag color="#abbac3" v-else>未审核</a-tag>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template v-if="info?.status == 0">
            <a-popconfirm title="是否确认删除该跟进记录？" @confirm="handleFollowDelete(record)">
              <a-button type="danger" size="small">删除</a-button>
            </a-popconfirm>
          </template>
        </span>
      </a-table>
    </a-card>
    <div style="width: 0; height: 0; overflow: hidden">
      <div id="printMe" class="objectDialogFlowPrint">
        <div class="myPrint">
          <detailList :info="this.info"/>
        </div>
      </div>
    </div>
  </page-header-wrapper>
</template>

<script>
import {
  getAfterSalesFollowUpListUsingPOST,
  getAfterSalesRecordUsingPOST,
  revokeSvOrderUsingPOST,
  finishSvOrderUsingPOST,
  deleteSvOrderUsingPOST,
  deleteAfterSalesFollowUpUsingPOST,
  exportAfterSalesFollowUpListUsingPOST
} from '@/api/api/shouhouguanli.ts'
import detailList from './tuiDetail.vue'
export default {
  name: 'ServiceServiceRecordDetailsDetail',
  components: {
    detailList
  },
  data() {
    return {
      printOption: {
        id: 'printMe', // 打印元素的id 不需要携带#号
        // preview: true, // 开启打印预览
        previewTitle: '打印预览', // 打印预览标题
        popTitle: '售后详情 - 达臻贸易', // 页眉标题 默认浏览器标题 空字符串时显示undefined 使用html语言
      },
      info: {},
      mdl: null,
      visible: false,
      confirmLoading: false,
      turnMdl: null,
      turnVisible: false,
      turnConfirmLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        orderId: this.$route.params.id,
        status: '',
      },
      loading: false,
      pagination: {},
      advanced: false,
      columns: [
        {
          title: '跟进日期',
          dataIndex: 'recDate',
        },
        {
          title: '类型',
          dataIndex: 'recType',
          scopedSlots: { customRender: 'recType' },
        },
        {
          title: '摘要',
          dataIndex: 'description',
        },
        {
          title: '详细描述',
          dataIndex: 'detail',
        },
        {
          title: '维修明细',
          dataIndex: 'memo',
        },
        {
          title: '售价(元)',
          dataIndex: 'price',
        },
        {
          title: '跟进人',
          dataIndex: 'empName',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loadData: [],
    }
  },
  created() {
    this.getDetail()
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleExpert(params){
      exportAfterSalesFollowUpListUsingPOST({ ...params }).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '售后跟进记录列表' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })  
    },
    handlePayment(id){
      this.$router.push({ path: `/service/servicerecord/order/${id}` })
    },
    getDetail() {
      getAfterSalesRecordUsingPOST({ id: this.$route.params.id })
        .then((res) => {
          this.info = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleSearch(params) {
      this.loading = true
      getAfterSalesFollowUpListUsingPOST(params)
        .then((res) => {
          this.loadData = res.data.list
          this.loading = false
          this.queryParam.pageNo = this.pagination.current
          this.pagination = { ...this.pagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.loading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        orderId: this.$route.params.id,
        queryCondition: this.queryParam.queryCondition,
        status: this.queryParam.status,
        date: this.queryParam.date,
      })
    },
    handleBack() {
      const that = this
      that.$confirm({
        title: '撤销',
        content: '你确定撤销这个售后吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          revokeSvOrderUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.getDetail()
              that.handleSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleFinish() {
      const that = this
      that.$confirm({
        title: '完成',
        content: '你确定售后完成了吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          finishSvOrderUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.getDetail()
              that.handleSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleDelete() {
      const that = this
      that.$confirm({
        title: '删除',
        content: '你确定删除这个售后吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          deleteSvOrderUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.$router.back()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleFollowDelete(record) {
      deleteAfterSalesFollowUpUsingPOST({ id: record.id })
        .then(() => {
          this.getDetail()
          this.handleSearch(this.queryParam)
          this.$message.success('操作成功')
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleAddFollow() {
      this.$router.push({ path: `/service/servicerecord/addfollow/${this.$route.params.id}` })
    },
  },
}
</script>
